<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="blockRules" :model="blockForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="blockForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="blockForm.title" />
      </el-form-item>
      <el-form-item label="应用" prop="blockclass">
        <el-input v-model="blockForm.blockclass" />
      </el-form-item>
      <el-form-item label="设置" prop="setting">
        <el-input v-model="blockForm.setting" />
      </el-form-item>
      <el-form-item label="缓存时间" prop="cacheTime">
        <el-input-number v-model="blockForm.cacheTime" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>
        <el-select v-model="blockForm.cacheType" placeholder="请选择">
          <el-option
            v-for="item in cacheTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="blockForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="blockForm.status.value === 5" type="danger" @click="handleDelete(blockForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveBlock, removeBlockByID, getBlock } from '@/api/pagination/block'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      cacheTypeOptions:[
        {
          label:"日",
          value:1
        },
        {
          label:"时",
          value:2
        },
        {
          label:"分",
          value:3
        }
      ],
      blockForm: {
        code: '',
        title: '',
        cacheTime:0,
        cacheType:1,
        remark: '',
        status: { label: '', value: 1 }
      },
      blockRules: []
    }
  },
  created() {
    let blockID = this.$route.query && this.$route.query.blockID
    if (blockID !== undefined) {
      blockID = parseInt(blockID)
      if (!isNaN(blockID)) {
        this.listLoading = true
        const formData = {
          'blockID': blockID
        }
        getBlock(formData).then(response => {
          if (response.status === 200) {
            this.blockForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(blockID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'block' })
    },
    handleChange:function(e){
      console.log(e)
    },
    createData() {
      this.listLoading = true

      const _blockForm = this.blockForm
      try {
        _blockForm.status = _blockForm.status.value
      } catch (error) {
        _blockForm.status = 6
      }
      if (_blockForm.ID > 0) {
        _blockForm.blockID = _blockForm.ID
      }
      saveBlock(_blockForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'block' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.$confirm('确实要执行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const formData = {
          'blockID': row.ID
        }
        removeBlockByID(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'group' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
